<?php
/*
 * @Descripttion: 
 * @Author: 夜雨
 * @Email: 448408740@qq.com
 * @Date: 2019-11-25 15:25:26
 * @LastEditTime: 2019-12-04 11:09:56
 */
namespace app\admin\model;

use think\Model;
use think\facade\Log;

class Message extends Model
{
    protected $table = 'bsa_message';

    /**
     * @msg: 获取留言
     * @param int $limit
     * @param string $where
     * @return: json
     */
    public function getMessages($limit, $where)
    {

        try {

            $res = $this->field('id,dianhua,xingming,neirong,keyword,add_time,status')->where($where)
                ->order('id', 'desc')->paginate($limit);

        }catch (\Exception $e) {
            Log::error($e->getMessage());
            return modelReMsg(-1, '', $e->getMessage());
        }

        return modelReMsg(0, $res, 'ok');
    }

    /**
     * @msg: 根据ID 获取留言信息
     * @param int $id 
     * @return: json
     */
    public function getMessageById($id)
    {
        try {

            $res = $this->where('id', $id)->find();

        }catch (\Exception $e) {
            Log::error($e->getMessage());
            return modelReMsg(-1, '', $e->getMessage());
        }

        return modelReMsg(0, $res, '获取留言成功');
    }

    /**
     * @msg: 编辑留言
     * @param array $param
     * @return array
     */
    public function editMessage($param)
    {
        try {
            $this->where('id', $param['id'])->update($param);
        }catch (\Exception $e) {
            Log::error($e->getMessage());
            return modelReMsg(-1, '', $e->getMessage());
        }

        return modelReMsg(0, '', '编辑留言成功');
    }

    /**
     * @msg: 添加留言
     * @param array $param
     * @return array
     */
    public function addMessage($param)
    {
        try {
            $param['addtime'] = time();
            $param['add_time'] = date('Y-m-d H:i:s');
            $this->insert($param);
        }catch (\Exception $e) {
            Log::error($e->getMessage());
            return modelReMsg(-1, '', $e->getMessage());
        }
        return modelReMsg(0, '', '添加留言成功');
    }

    /**
     * @msg: 删除留言
     * @param int $id
     * @return array
     */
    public function delMessageById($id)
    {
        try {
            $this->where('id', $id)->delete();
        } catch (\Exception $e) {
            Log::error($e->getMessage());
            return modelReMsg(-1, '', $e->getMessage());
        }

        return modelReMsg(0, '', '删除成功');
    }

    /**
     * @msg: 审核留言
     * @param int $id
     * @return array
     */
    public function statusMessageById($id)
    {
        try {
            $this->where('id', $id)->update(['status' => 1]);
        } catch (\Exception $e) {
            Log::error($e->getMessage());
            return modelReMsg(-1, '', $e->getMessage());
        }

        return modelReMsg(0, '', '审核成功');
    }

    /**
     * @msg: 统计
     * @param array $where
     * @return array
     */
    public function chartMessageCount($args = [])
    {
        $where = [];
        if (empty($args['start_date'])) $args['start_date'] = date('Y-m-d', strtotime('-6 day'));
        if (empty($args['end_date'])) $args['end_date'] = date('Y-m-d', strtotime('+1 day'));
        if (!empty($args['ism'])) $where[] = ['ism', 'eq', $args['ism']];
        $format = "'%Y-%m-%d'";
        if(substr_count($args['start_date'], '-') == 1){
            $format = "'%Y-%m'";
        }
        $res = $this->field("DATE_FORMAT(add_time, $format) adddate, COUNT(*) count")->whereTime('add_time', 'between', [$args['start_date'], $args['end_date']])->where($where)->group('adddate')->select();

        $temp = [];
        foreach($res as $v){
            $temp[$v['adddate']] = $v['count'];
        }
        unset($res);
        $list = dateSplit($args['start_date'], $args['end_date']);
        foreach($list as $k => $v){
            if(isset($temp[$k])) $list[$k] = $temp[$k];
        }
        return $list;
    }
    
}